home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / IndentMaster / Source / IndentController.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  11.4 KB  |  402 lines

  1. /* IndentController.m 
  2.  * Copyright (C) 1994 Kurt Werle
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU General Public License, see the file COPYING.
  5.  */
  6.  
  7. #import "IndentController.h"
  8.  
  9. @implementation IndentController
  10.  
  11. - setFileName:(char *)newFileName
  12. {
  13.     myFileName = (char *)malloc (strlen (newFileName) + 1);
  14.     if (myFileName == NULL)
  15.     {
  16.     NXRunAlertPanel (NULL, "You're outta memory!!!\nAborting Program.", "OK", NULL, NULL);
  17.     exit (0);
  18.     }
  19.     strcpy (myFileName, newFileName);
  20.     return self;
  21. }
  22.  
  23. - initValues
  24. {
  25.     [BlankafterBlock setIntValue:0];
  26.     [BlankafterProcedure setIntValue:0];
  27.     [BlankbeforeBlock setIntValue:0];
  28.     [NewLineafterComma setIntValue:0];
  29.     [LineupCompound setIntValue:0];
  30.     [BlankLineComments setIntValue:1];
  31.     [NestleElses setIntValue:1];
  32.     [LeftJustifyDeclares setIntValue:0];
  33.     [MatchElsetoIf setIntValue:1];
  34.     [FormatCol1Comments setIntValue:1];
  35.     [IndentParameterDeclarations setIntValue:1];
  36.     [JustifyParenthesisSplit setIntValue:1];
  37.     [ConvertSymbols setIntValue:0];
  38.     [SpaceBeforeParens setIntValue:0];
  39.     [SpaceAroundPointers setIntValue:0];
  40.     [ProcedureNameCol1 setIntValue:1];
  41.     [LineComments setIntValue:1];
  42.     [DeleteExtraBlanks setIntValue:0];
  43.     [VerboseMode setIntValue:0];
  44.  
  45.     [CommentCodeCol setIntValue:33];
  46.     [CommentDeclarationCol setIntValue:33];
  47.     [ContinuationIndent setIntValue:0];
  48.     [IndentCaseLabels setFloatValue:0.0];
  49.     [NegativeIndentComments setIntValue:0];
  50.     [IndentVariableDeclarations setIntValue:16];
  51.     [SpaceperIndent setIntValue:8];
  52.     [MaxLineLength setIntValue:78];
  53.     [MaxCommentLineLength setIntValue:78];
  54.     [TabStopWidth setIntValue:8];
  55.     return self;
  56. }
  57.  
  58. - (const char *)OptionString
  59. {
  60.     char            allTypeDefText[2048];
  61.  
  62.     /* So Now we get Ugly... */
  63.  
  64.     [typedefText getSubstring:allTypeDefText start:0 length:[typedefText textLength] + 1];
  65.     sprintf (indentCodes, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s -c%d -cd%d -ci%d -cli%f -d%d -di%d -i%d -l%d -lc%d -ts%d %s",
  66.          [BlankafterBlock intValue] ? "-bad" : "-nbad",
  67.          [BlankafterProcedure intValue] ? "-bap" : "-nbap",
  68.          [BlankbeforeBlock intValue] ? "-bbb" : "-nbbb",
  69.          [NewLineafterComma intValue] ? "-bc" : "-nbc",
  70.          [LineupCompound intValue] ? "-br" : "-bl",
  71.          [BlankLineComments intValue] ? "-cdb" : "-ncdb",
  72.          [NestleElses intValue] ? "-ce" : "-nce",
  73.          [LeftJustifyDeclares intValue] ? "-dj" : "-ndj",
  74.          [MatchElsetoIf intValue] ? "-ei" : "-nei",
  75.          [FormatCol1Comments intValue] ? "-fc1" : "-nfc1",
  76.          [IndentParameterDeclarations intValue] ? "-ip" : "-nip",
  77.          [JustifyParenthesisSplit intValue] ? "-lp" : "-nlp",
  78.          [ConvertSymbols intValue] ? "-osa" : "-nosa",
  79.          [SpaceBeforeParens intValue] ? "-pcs" : "-npcs",
  80.          [SpaceAroundPointers intValue] ? "-ps" : "-nps",
  81.          [ProcedureNameCol1 intValue] ? "-psl" : "-npsl",
  82.          [LineComments intValue] ? "-sc" : "-nsc",
  83.          [DeleteExtraBlanks intValue] ? "-sob" : "-nsob",
  84.          [VerboseMode intValue] ? "-v" : "-nv",
  85.  
  86.          [CommentCodeCol intValue],
  87.          [CommentDeclarationCol intValue],
  88.          [ContinuationIndent intValue],
  89.          [IndentCaseLabels floatValue],
  90.          [NegativeIndentComments intValue],
  91.          [IndentVariableDeclarations intValue],
  92.          [SpaceperIndent intValue],
  93.          [MaxLineLength intValue],
  94.          [MaxCommentLineLength intValue],
  95.          [TabStopWidth intValue],
  96.          allTypeDefText
  97.     );
  98.  
  99.     return indentCodes;
  100. }
  101.  
  102. #define MAXTOKENLENGTH 80    /* If you have tokens over 80 chars */
  103. #define FSCANF_FORMAT_STRING "%79s"    /* You deserve what you get. */
  104.  
  105. - (void)parseIndentFile:(char *)indentFileName
  106. {
  107.     char            indentToken[MAXTOKENLENGTH], sometypedefText[MAXTOKENLENGTH];
  108.     FILE           *indentFile;
  109.     int             tempint;
  110.     float           tempfloat;
  111.  
  112.     indentFile = fopen (indentFileName, "rt");
  113.     [self setFileName:indentFileName];
  114.     strcpy (myFileName, indentFileName);
  115.  
  116.     [typedefText selectAll:self];
  117.     [typedefText replaceSel:""];
  118.  
  119.     while (fscanf (indentFile, FSCANF_FORMAT_STRING, indentToken) > 0)
  120.     {
  121.     /* We only need to test for the 'non-default' case */
  122.     if (strstr (indentToken, "-bad"))
  123.         [BlankafterBlock setIntValue:1];
  124.     if (strstr (indentToken, "-bap"))
  125.         [BlankafterProcedure setIntValue:1];
  126.     if (strstr (indentToken, "-bbb"))
  127.         [BlankbeforeBlock setIntValue:1];
  128.     if (strstr (indentToken, "-bc"))
  129.         [NewLineafterComma setIntValue:1];
  130.     if (strstr (indentToken, "-bl"))
  131.         [LineupCompound setIntValue:0];
  132.     if (strstr (indentToken, "-ncdb"))
  133.         [BlankLineComments setIntValue:0];
  134.     if (strstr (indentToken, "-nce"))
  135.         [NestleElses setIntValue:0];
  136.     if (strstr (indentToken, "-dj"))
  137.         [LeftJustifyDeclares setIntValue:1];
  138.     if (strstr (indentToken, "-nei"))
  139.         [MatchElsetoIf setIntValue:0];
  140.     if (strstr (indentToken, "-nfc1"))
  141.         [FormatCol1Comments setIntValue:0];
  142.     if (strstr (indentToken, "-nip"))
  143.         [IndentParameterDeclarations setIntValue:0];
  144.     if (strstr (indentToken, "-nlp"))
  145.         [JustifyParenthesisSplit setIntValue:0];
  146.     if (strstr (indentToken, "-osa"))
  147.         [ConvertSymbols setIntValue:1];
  148.     if (strstr (indentToken, "-pcs"))
  149.         [SpaceBeforeParens setIntValue:1];
  150.     if (strstr (indentToken, "-ps"))
  151.         [SpaceAroundPointers setIntValue:1];
  152.     if (strstr (indentToken, "-npsl"))
  153.         [ProcedureNameCol1 setIntValue:0];
  154.     if (strstr (indentToken, "-nsc"))
  155.         [LineComments setIntValue:0];
  156.     if (strstr (indentToken, "-sob"))
  157.         [DeleteExtraBlanks setIntValue:1];
  158.     if (strstr (indentToken, "-v"))
  159.         [VerboseMode setIntValue:1];
  160.  
  161.     if (sscanf (indentToken, "-c%d", &tempint))
  162.         [CommentCodeCol setIntValue:tempint];
  163.     if (sscanf (indentToken, "-cd%d", &tempint))
  164.         [CommentDeclarationCol setIntValue:tempint];
  165.     if (sscanf (indentToken, "-ci%d", &tempint))
  166.         [ContinuationIndent setIntValue:tempint];
  167.     if (sscanf (indentToken, "-cli%f", &tempfloat))
  168.         [IndentCaseLabels setFloatValue:tempfloat];
  169.     if (sscanf (indentToken, "-d%d", &tempint))
  170.         [NegativeIndentComments setIntValue:tempint];
  171.     if (sscanf (indentToken, "-di%d", &tempint))
  172.         [IndentVariableDeclarations setIntValue:tempint];
  173.     if (sscanf (indentToken, "-i%d", &tempint))
  174.         [SpaceperIndent setIntValue:tempint];
  175.     if (sscanf (indentToken, "-l%d", &tempint))
  176.         [MaxLineLength setIntValue:tempint];
  177.     if (sscanf (indentToken, "-lc%d", &tempint))
  178.         [MaxCommentLineLength setIntValue:tempint];
  179.     if (sscanf (indentToken, "-ts%d", &tempint))
  180.         [TabStopWidth intValue];
  181.  
  182.     if (sscanf (indentToken, "-T%s", sometypedefText))
  183.     {
  184.         [typedefText setSel:0 :0];
  185.         [typedefText replaceSel:" "];
  186.         [typedefText setSel:0 :0];
  187.         [typedefText replaceSel:indentToken];
  188.     }
  189.     }
  190. }
  191.  
  192. - appDidInit:sender
  193. {
  194.  
  195.     [self setFileName:"./.indent.pro"];
  196.     myCodeBlock = NXOpenMemory (NULL, 0, NX_READWRITE);
  197.     [myCodeScrollView writeText:myCodeBlock];
  198.     NXSeek (myCodeBlock, 0, NX_FROMSTART);
  199.     myFileName = NULL;
  200.     return self;
  201. }
  202.  
  203. - (BOOL)appAcceptsAnotherFile:sender
  204. {
  205.     return YES;
  206. }
  207.  
  208. - (int)app:sender openFile:(const char *)filename type:(const char *)aType;
  209. {
  210.     [self parseIndentFile:filename];
  211.     return YES;
  212. }
  213.  
  214. - loadInfo:sender
  215. {
  216.     [NXApp loadNibSection:"info.nib" owner:self withNames:NO];
  217.     return self;
  218. }
  219.  
  220. - NewFile:sender
  221. {
  222.     [self initValues];
  223.     return self;
  224. }
  225.  
  226. - LoadFile:sender
  227. {
  228.     id              myopenpanel;
  229.     const char     *const types[2] = {"pro", NULL};
  230.  
  231.     myopenpanel = [OpenPanel new];
  232.  
  233.     [myopenpanel allowMultipleFiles:FALSE];
  234.     if (NX_OKTAG == [myopenpanel runModalForDirectory:"." file:".indent.pro" types:types])
  235.     {
  236.     [self initValues];
  237.  
  238.     [self parseIndentFile:[myopenpanel filename]];
  239.     }
  240.     return self;
  241. }
  242.  
  243. - ProcessCode:sender
  244. {
  245.     char            systemLine[2048];
  246.     NXStream       *tempCodeFileStream;
  247.     int             tempCodeFile;
  248.  
  249.     /*
  250.        This is totally bogus.  Why do I have to open this file TWICE?!? There
  251.        Should Be a command NXOpenMyDamnFile(const char *filename, int flags)! 
  252.     */
  253.     tempCodeFile = open (".indent.code.c", O_WRONLY | O_CREAT, 0777);
  254.  
  255. #ifdef DEBUG
  256.     if (tempCodeFile != -1)
  257.     printf ("opened %d\n", tempCodeFile);
  258.     else
  259.     perror ("bummer");
  260. #endif
  261.  
  262.     tempCodeFileStream = NXOpenFile (tempCodeFile, NX_WRITEONLY);
  263.     printf ("openedstream %p\n", tempCodeFileStream);
  264.  
  265.     [myCodeScrollView writeText:tempCodeFileStream];
  266.     /*
  267.        And while I'm ranting, can I just get a copy of the Text text so I can
  268.        do my own fopen/fprintf? 
  269.     */
  270.  
  271.     NXClose (tempCodeFileStream);
  272.     close (tempCodeFile);
  273.  
  274.     sprintf (systemLine, "indent -npro %s .indent.code.c",[self OptionString]);
  275.  
  276. #ifdef DEBUG
  277.     printf ("%s\n", systemLine);
  278. #endif
  279.  
  280.     /*
  281.        I wanted to do this using a pipe, and avoid the whole temporary file
  282.        thing, but pipes look real ugly.  Is there no easy way to say
  283.        'openmypipe (systemLine, &writefd, &readfd)? 
  284.     */
  285.     system (systemLine);
  286.  
  287.     tempCodeFile = open (".indent.code.c", O_RDONLY, 777);
  288.     tempCodeFileStream = NXOpenFile (tempCodeFile, NX_READONLY);
  289.     [myCodeScrollView readText:tempCodeFileStream];
  290.     NXClose (tempCodeFileStream);
  291.  
  292.     unlink (".indent.code.c");
  293.     unlink (".indent.code.c.BAK");
  294.  
  295.     return self;
  296. }
  297.  
  298. - ResetCode:sender
  299. {
  300.     [myCodeScrollView readText:myCodeBlock];
  301.     NXSeek (myCodeBlock, 0, NX_FROMSTART);
  302.     return self;
  303. }
  304.  
  305. - SaveFile:sender
  306. {
  307.     FILE           *mySaveFile;
  308.  
  309.     if (myFileName == NULL)
  310.     {
  311.     myFileName = "./.indent.pro";
  312.     return[self SaveFileAs:self];
  313.     }
  314.  
  315.     if (!access (myFileName, F_OK))    /* Rename the old file .BAK */
  316.     {
  317.     char           *newFileName = (char *)malloc (strlen (myFileName) + 5);
  318.  
  319.     if (newFileName == NULL)
  320.     {
  321.         NXRunAlertPanel (NULL, "You're outta memory!!!\nAborting Save.", "OK", NULL, NULL);
  322.         return self;
  323.     }
  324.  
  325.     strcpy (newFileName, myFileName);
  326.     strcat (newFileName, ".BAK");
  327.     unlink (newFileName);    /* Remove old .BAK file if it exists */
  328.     if (0 != rename (myFileName, newFileName))
  329.     {
  330.         if (NX_ALERTDEFAULT != NXRunAlertPanel (NULL, "Couldn't make backup.\nSave over file?", "OK", "Don't Hose me!", NULL))
  331.         {
  332.         free (newFileName);
  333.         return self;
  334.         }
  335.     }
  336.     free (newFileName);
  337.     }
  338.  
  339.     mySaveFile = fopen (myFileName, "wt");
  340.     if (!mySaveFile)
  341.     {
  342.     NXRunAlertPanel (NULL, "Couldn't open save file.\nAborting Save?", "OK", NULL, NULL);
  343.     return self;
  344.     }
  345.     if (0 != fprintf (mySaveFile, "%s\n",[self OptionString]))
  346.     NXRunAlertPanel (NULL, "Save failed.\nCouldn't write to opened file.", "OK", NULL, NULL);
  347.     fclose (mySaveFile);
  348.     return self;
  349. }
  350.  
  351. - SaveFileAs:sender
  352. {
  353.     id              mySavePanel;
  354.     char           *saveFilePath, *saveFileName;
  355.  
  356.     if (myFileName == NULL)
  357.     {
  358.     [self setFileName:"./.indent.pro"];
  359.     }
  360.  
  361.     saveFilePath = (char *)malloc (strlen (myFileName) + 1);
  362.     if (saveFilePath == NULL)
  363.     {
  364.     NXRunAlertPanel (NULL, "You're outta memory!!!\nAborting Save.", "OK", NULL, NULL);
  365.     return self;
  366.     }
  367.     strcpy (saveFilePath, myFileName);
  368.     if (NULL == (saveFileName = strchr (saveFilePath, '/')))
  369.     {
  370.     saveFileName = saveFilePath;
  371.     }
  372.     else
  373.     {
  374.     *saveFileName = '\0';
  375.     saveFileName++;
  376.     }
  377.  
  378.     mySavePanel = [SavePanel new];
  379.  
  380.     if (NX_OKTAG == [mySavePanel runModalForDirectory:saveFilePath file:saveFileName])
  381.     {
  382.     [self setFileName:[mySavePanel filename]];
  383.     free (saveFilePath);
  384.     return[self SaveFile:self];
  385.     }
  386.  
  387.     free (saveFilePath);
  388.     return self;
  389. }
  390.  
  391. - free
  392. {                /* I wonder why this is never called... */
  393.     if (myFileName != NULL)
  394.     {
  395.     free (myFileName);
  396.     }
  397.     NXCloseMemory (myCodeBlock, NX_TRUNCATEBUFFER);
  398.     return[super free];
  399. }
  400.  
  401. @end
  402.